﻿(function (FlashPay) {
    //UI命名空间
    var Util = FlashPay.Util || {};

    /*******************************
    * 重写Ajax
    *******************************/
    Util.Ajax = function (opt) {

        var fn = {
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (data, textStatus) { }
        }
        if (opt.error) {
            fn.error = opt.error;
        }
        if (opt.success) {
            fn.success = opt.success;
        }

        var _opt = $.extend(opt, {
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                fn.error(XMLHttpRequest, textStatus, errorThrown);
            },
            success: function (data, textStatus) {
                try {
                    if (data.length < 500) {
                        var dataJson = $.parseJSON(data);
                        if (dataJson.Error) {
                            FlashPay.UI.Alert({ content: dataJson.Message });
                            data = "";
                        }
                    }
                    if (typeof (data) === "object") {
                        if (data.callback != undefined && data.callback != "") {
                            eval(data.callback);
                        }
                    }
                    if (data && data.IsLogout) {
                        eval(data.Callback);
                    }
                } catch (e) {
                }
                fn.success(data, textStatus);
            }
        });

        $.ajax(_opt);


    };

    /*******************************
    * 获取参数
    *******************************/
    Util.GetQueryString = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    /*******************************
    * 把url的参数部分转化成json对象
    *******************************/
    Util.parseQueryString = function (url) {

        var reg_url = /^[^\?]+\?([\w\W]+)$/,
            reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
            arr_url = reg_url.exec(url),
            json = {};
        if (arr_url && arr_url[1]) {
            var str_para = arr_url[1], result;
            while ((result = reg_para.exec(str_para)) != null) {
                json[result[1]] = result[2];
            }
        }
        return json;
    }

    /*******************************
   * 把url的参数部分转化成json对象,为空不转为对象
   *******************************/
    Util.parseQueryString2 = function (url) {
        var obj = {};
        var keyvalue = [];
        var key = "",
            value = "";
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
        for (var i in paraString) {
            keyvalue = paraString[i].split("=");
            key = keyvalue[0];
            value = keyvalue[1];
            if (value != "") {
                obj[key] = value;
            }
        }
        return obj;
    }


    Util.Validator = function (options) {
        var defaults = {
            formObj: null,
            rules: {},
            messages: {}
        };
        // 处理默认参数
        var opts = $.extend({}, defaults, options);

        return opts.formObj.validate({
            ignore: "",
            rules: opts.rules,
            messages: opts.messages,
            errorElement: "em",
            errorPlacement: function (error, element) {
                FlashPay.Util.Tip_popover({
                    element: element,
                    content: error[0].innerText,
                    isShow: true
                });
            },
            success: function (label, element) {
                FlashPay.Util.Tip_popover({
                    element: $(element),
                    isShow: false
                });
            }
        });
    };

    /*******************************
    * 设置Input值
    *******************************/
    Util.SetInputVal = function (name, value) {
        $("input[name=" + name + "]").val(value)
    }

    //验证是否为空
    Util.isNullOrEmptySpance = function (value) {
        if (Object.prototype.toString.call(value) === "[object String]") {
            if ((value == null || value.trim() == "" || value == 'undefined' || value == 'null'))
                return true;
        } else {
            if ((value == null || value == "" || value == 'undefined' || value == 'null'))
                return true;
        }
        return false;
    }

    Util.Tip_popover = function (options) {
        var defaults = {
            element: null,
            content: "",
            isShow: false
        };

        // 处理默认参数
        var opts = $.extend({}, defaults, options);

        if (opts.element == null) {
            return;
        }
        if (opts.isShow && FlashPay.Util.isNullOrEmptySpance(opts.content)) {
            return;
        }
        var objSpan = opts.element.next("div")[0];
        if (opts.isShow && !opts.element.parent().hasClass("has-error")) {
            opts.element.parent().addClass("has-error has-feedback");

            if (!objSpan) {
                objSpan = $("<div class=\"popoverDiv\" data-placement=\"bottom\" href=\"javascript:void(0);\"></div>");
                objSpan.insertAfter(opts.element);
                objSpan.popover();
            }

            $(objSpan).attr('data-container', 'body');
            $(objSpan).attr('data-toggle', 'popover');
            $(objSpan).attr('data-placement', 'bottom');
            $(objSpan).attr('data-content', opts.content);

            objSpan.click();
        }

        if (!opts.isShow && objSpan) {
            objSpan.click();

            opts.element.parent().removeClass("has-error has-feedback");
            objSpan.remove();
        }
    };

    //显示状态值
    Util.GetStatusValue = function (status) {
        if (FlashPay.Util.isNullOrEmptySpance(status)) return "";
        switch (status) {
            case 1:
                return "<span class=\"green bold\">启用</span>";
                break;
            case 2:
                return "<span class=\"red bold\">禁用</span>";
                break;
            case 3:
                return "<span class=\"red bold\">删除</span>";
                break;
        }
        return "";
    };

    //角色显示状态值
    Util.GetRoleStatusValue = function (status) {
        if (FlashPay.Util.isNullOrEmptySpance(status)) return "";
        switch (status) {
            case 1:
                return "显示";
                break;
            case 2:
                return "隐藏";
                break;
        }
        return "";
    };
    //银行卡类型
    Util.GetCardTypeValue = function (CardType) {
        if (FlashPay.Util.isNullOrEmptySpance(CardType)) return "";
        switch (CardType) {
            case 1:
                return "收款卡";
                break;
            case 2:
                return "中转卡";
                break;
            case 3:
                return "付款卡";
                break;
        }
        return "";
    }
    //跨行转账
    Util.GetCrossBankPayValue = function (CrossBankPay) {
        if (FlashPay.Util.isNullOrEmptySpance(CrossBankPay)) return "";
        switch (CrossBankPay) {
            case 1:
                return "同行";
                break;
            case 2:
                return "跨行";
                break;
            case 3:
                return "全部";
                break;
        }
        return "";
    }
    //银行卡号截取显示
    Util.SubCardNumber = function (CardNumber) {
        if (Util.isNullOrEmptySpance(CardNumber)) return "";
        var StaNum = CardNumber.substring(0, 4);
        var EndNum = CardNumber.substr(CardNumber.length-4);
        return StaNum + "***********" + EndNum;
    }
    //付款区间起迄
    Util.ViewPaymentSE = function (PaymentStart, PaymentEnd) {
        var StaNum = PaymentStart;
        var EndNum = PaymentEnd;
        return "EX" + StaNum + "-" + EndNum;
    }

    //当前平台:卡管后台: 1,秒付宝存款: 2,秒付宝付款: 3
    Util.Platform = function () {
        return 1;
    }
    //银行卡使用状态
    Util.GetCardStatusValue = function (CardStatus) {
        if (FlashPay.Util.isNullOrEmptySpance(CardStatus)) return "";
        switch (CardStatus) {
            case 1:
                return "有效";
                break;
            case 2:
                return "冻结";
                break;
        }
        return "";
    }
    //银行卡启用状态
    Util.GetCardEnableStatusValue = function (CardEnableStatus) {
        if (FlashPay.Util.isNullOrEmptySpance(CardEnableStatus)) return "";
        switch (CardEnableStatus) {
            case 1:
                return " <span class='green bold'>启用</span> ";
                break;
            case 2:
                return " <span class='red bold'>禁用</span> ";
                break;
            case 3:
                return "删除";
                break;
        }
        return "";
    }

    //将时间截取成日期
    Util.GetDateByTime = function (time, type = 0) {
        time = time.replace("T", " ");
        if (type != 0) return time;
        return time.substr(0, 10);
    }

    //截取字符串加上...
    Util.GetShortStr = function (str, len = 20) {
        if (Util.isNullOrEmptySpance(str)) return "";
        if (str.length > len) {
            return str.substr(0, len) + "...";
        } else {
            return str.substr(0, len);
        }
    }

    //导入到全局FlashPay中
    FlashPay.Util = Util;

})(FlashPay);